package Dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DataBaseLink {
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getMysqlConn(){
        String url ="jdbc:mysql://localhost:3306/employeesys?serverTimezone=UTC";
        String userName = "root";
        String password = "0304";

        try {
            Connection conn = DriverManager.getConnection(url,userName,password);
            if (conn != null){
                System.out.println("数据库连接成功");
                return conn;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void closeConnection(PreparedStatement ps, Connection connection){
        if (ps != null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
